package de.jmda.gen;

import de.jmda.core.util.StringUtil;

public class LineIndenter
{
	public final static LineIndenter TAB_SINGLE = new LineIndenter("\t", 1);
	public final static LineIndenter TAB_DOUBLE = new LineIndenter("\t", 2);
	public final static LineIndenter TAB_TRIPLE = new LineIndenter("\t", 3);

	/**
	 * String used for indentation of lines in {@link #indent(String)} and {@link
	 * #indent(StringBuffer)}.
	 */
	private String indentation;
	
	/**
	 * Number of times {@link #indentation} is used for indenting.
	 */
	private int indentationLevel;

	/**
	 * Switch that turns indentation on (<code>true</code>) and off (<code>false
	 * </code>)
	 */
	private boolean indenting;

	/**
	 * @param indentation
	 * @param indentationLevel values &lt; 0 will be handled as 0
	 * @param indenting
	 * 
	 * @see #indentation
	 * @see #indentationLevel
	 * @see #indenting
	 */
	public LineIndenter(
			String indentation, int indentationLevel, boolean indenting)
  {
	  super();

	  setIndentation(indentation);
	  setIndentationLevel(indentationLevel);
	  setIndenting(indenting);
  }
	
	/**
	 * Shortcut to {@link LineIndenter#LineIndenter(String, int, boolean)} with
	 * the parameter values <code>indentation</code>, <code>indentationLevel
	 * </code> and <code>true</code>. The line indenter will indent lines.
	 *
	 * @param indentation
	 * @param indentationLevel
	 */
	public LineIndenter(String indentation, int indentationLevel)
  {
		this(indentation, indentationLevel, true);
  }

	/**
	 * Shortcut to {@link LineIndenter#LineIndenter(String, int, boolean)} with
	 * the parameter values <code>""</code>, <code>0</code> and <code>false
	 * </code>. The line indenter will not indent lines.
	 */
	public LineIndenter()
	{
		this("", 0, false);
	}

	public String getIndentation()
  {
  	return indentation;
  }

	/**
	 * @param indentation <code>null</code> values are handled as <code>""</code>
	 */
	public void setIndentation(String indentation)
  {
		if (indentation == null)
		{
			this.indentation = "";
		}
		else
		{
	  	this.indentation = indentation;
		}
  }

	public int getIndentationLevel()
  {
  	return indentationLevel;
  }

	/**
	 * @param indentationLevel values &lt; 0 are handled as 0
	 */
	public void setIndentationLevel(int indentationLevel)
  {
		if (indentationLevel < 0)
		{
			this.indentationLevel = 0;
		}
		else
		{
			this.indentationLevel = indentationLevel;
		}
  }

	public void setIndenting(boolean indenting)
	{
		this.indenting = indenting;
	}

	public boolean getIndenting()
	{
		return indenting;
	}
	
	public String indent(String string)
	{
		if (indenting)
		{
			return StringUtil.indent(string, indentation, indentationLevel);
		}
		
		return string;
	}
	
	public StringBuffer indent(StringBuffer stringbuffer)
	{
		if (indenting)
		{
			return StringUtil.indent(stringbuffer, indentation, indentationLevel);
		}
		
		return stringbuffer;
	}
}